import 'package:situation/data/store/auth_store.dart';
import 'package:situation/domain/auth/UserIdentity.dart';
import 'package:situation/domain/auth/login_token.dart';
import 'package:situation/domain/auth/login_user.dart';
import 'package:situation/domain/common/result.dart';
import 'package:situation/domain/user/ElectronIdCardDto.dart';
import 'package:situation/domain/user/OrderDto.dart';
import 'package:situation/domain/user/ThreeLoginDto.dart';
import 'package:situation/domain/user/UserInformation.dart';
import 'package:situation/shared/shared.dart';
import 'package:dio/dio.dart';

/// 用户
class UserRequest {
  /// 通过手机号密码登录
  static Future<LoginToken> loginByPassword(mobile, password) async {
    var response = await UtilHttp().post(
      '/auth/appLogin',
      data: {"username": mobile, "password": password},
    );
    return LoginToken.fromJson(response['data']);
  }

  /// 获取用户信息
  static Future<LoginUser> profile() async {
    var response = await UtilHttp().get('/auth/getLoginInfo');
    return LoginUser.fromJson(response['data']);
  }

  // 发送找回密码验证码
  static Future sendForgotPwdCode(mobile) async {
    var response = await UtilHttp().get('/user/sendForgotPwdCode/' + mobile);
    return response;
  }

  // 注册
  static Future<Result> register(username, mobile, smsCode, password) async {
    var response = await UtilHttp().post(
      '/user/users/register',
      data: {
        "username": username,
        "mobile": mobile,
        "smsCode": smsCode,
        "password": password,
        "type": "APP",
      },
    );
    return Result.fromJson(response);
  }

  // 找回密码
  static Future<Result> forgotPwd(mobile, smsCode, password) async {
    var response = await UtilHttp().post(
      '/user/upPwdByMobile',
      data: {"mobile": mobile, "smsCode": smsCode, "password": password},
    );
    return Result.fromJson(response);
  }

  // 修改密码
  static Future<Result> upPwdByPwd(mobile, password, rePassword) async {
    var response = await UtilHttp().post(
      '/user/upPwdByPwd',
      data: {"mobile": mobile, "password": password, "newPassword": rePassword},
    );
    return Result.fromJson(response);
  }

  // 退出登录
  static Future logout() async {
    return await UtilHttp().post('/user/logout');
  }

  // 用户协议
  static Future<String> getProtocol() async {
    var response = await UtilHttp().get(
      '/app/appProtocol/selectAllByCode/yhxy',
    );
    return response['content'];
  }

  // 隐私政策
  static Future<String> getPriviacyPolicy() async {
    var response = await UtilHttp().get(
      '/app/appProtocol/selectAllByCode/yszc',
    );
    return response['content'];
  }

  // 隐私政策
  static Future<UserIdentity> selectUserIdentityInfoByIdcard() async {
    var response = await UtilHttp().get(
      '/app/userIdentity/selectUserIdentityInfoByIdcard',
    );
    return UserIdentity.fromJson(response['data']);
  }

  // 获取个人资料
  static Future<UserInformation> getUserInformation() async {
    var response = await UtilHttp().get('/app/appUser/queryByIdCard');
    return UserInformation.fromJson(response['data']);
  }

  // 完善资料
  static Future<Result> perfectManyInfo(data) async {
    var response = await UtilHttp().post(
      '/app/appUser/perfectManyInfo',
      data: data,
    );
    return Result.fromJson(response);
  }

  // 更新头像或昵称
  static Future<Result> updateNickNameOrAvatar(data) async {
    var response = await UtilHttp().post(
      '/app/andriod/user/updateUserNickNameAndHeadImgBySysUserId',
      data: data,
    );
    return Result.fromJson(response);
  }

  // 获取我的订单列表
  static Future<List<OrderDto>> getMyOrderList(page, size) async {
    var response = await UtilHttp().get(
      '/app/andriod/shopOrder/getUserShopOrderList?pageNumber=${page}&pageSize=${size}',
    );
    return response['data']['records']
        .map<OrderDto>((item) => OrderDto.fromJson(item))
        .toList();
  }

  // 意见反馈
  static Future<Result> userFeedback(data) async {
    var response = await UtilHttp().post('/app/userFeedback', data: data);
    return Result.fromJson(response);
  }

  // 电子身份证
  static Future<ElectronIdCardDto> generateElectronicIdcard(idcard) async {
    var response = await UtilHttp().get(
      '/app/electronicIdcard/generateElectronicIdcard?idcard=' + idcard,
    );
    return ElectronIdCardDto.fromJson(response['data']);
  }

  static Future<List<ThreeLoginDto>> getThreeLogin() async {
    var response = await UtilHttp().get('/user/getThreeLogin');
    return response['data']
        .map<ThreeLoginDto>((item) => ThreeLoginDto.fromJson(item))
        .toList();
  }

  //
  static Future<Result> scanConfirm(key) async {
    var response = await UtilHttp().get('/uaa/scan/scanConfirm?key=' + key);
    return Result.fromJson(response['data']);
  }

  static Future<Result> scanCodeUserLogin(key) async {
    var response = await UtilHttp().get(
      '/uaa/scan/scanCodeUserLogin?key=${key}&userId=${AuthStore.to.profile.id}',
    );
    return Result.fromJson(response['data']);
  }
}
